package com.xmg.boot.demo.bootlesson1.user.config;

import com.xmg.boot.demo.bootlesson1.user.intercept.JWTInterceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * @Author: ma
 * @CreateTime: 2023-11-23
 * @Description: JWT拦截器
 * @Version: 1.0
 */
@Configuration
public class IntercaptorConfig implements WebMvcConfigurer {

    @Autowired
    private JWTInterceptors jwtInterceptors;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除拦截，除了注册登录(此时还没token)，其他都拦截
        //excludePath.add("/login1");    //request路径，如login?name=1&pwd=1
        excludePath.add("/login/**");  // path路径，如 login/1/2
        excludePath.add("/doc.html");     //swagger
        excludePath.add("/swagger-ui.html");     //swagger
        excludePath.add("/swagger-resources/**");     //swagger
        excludePath.add("/v2/api-docs");     //swagger
        excludePath.add("/webjars/**");     //swagger
        registry.addInterceptor(jwtInterceptors)
                //拦截的路径
                .addPathPatterns("/**")
                //排除定义路径
                .excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);
    }
    /**
     * 解决跨域请求
     */
    // @Override
    // public void addCorsMappings(CorsRegistry registry) {
    //     registry.addMapping("/**")
    //             .allowedHeaders("*")
    //             .allowedMethods("*")
    //             //.allowedOrigins("*")
    //             .allowedOriginPatterns("*")
    //             .allowCredentials(true);
    // }

    /**
     * 异步请求配置
     * @param configurer
     */
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

}
